<?php

/**
 * 物流自提服务站父类
 *
 *
 * @网店运维提供技术支持 授权请购买shopnc授权
 * @license    http://www.shopwwi.com
 * @link       交流群号：111731672
 */




defined('ByShopWWI') or exit('Access Invalid!');



class Control{

    /**  
     * 检查短消息数量
     *
     */
    protected function checkMessage() {

        if($_SESSION['member_id'] == '') return ;

        //判断cookie是否存在

        $cookie_name = 'msgnewnum'.$_SESSION['member_id'];

        if (cookie($cookie_name) != null){

            $countnum = intval(cookie($cookie_name));

        }else {

            $message_model = Model('message');

            $countnum = $message_model->countNewMessage($_SESSION['member_id']);

            setNcCookie($cookie_name,"$countnum",2*3600);//保存2小时

        }

        Tpl::output('message_num',$countnum);

    }



    /**
     *  输出头部的公用信息
     *
     */
    protected function showLayout() {

        $this->checkMessage();//短消息检查

        $this->article();//文章输出



        $this->showCartCount();



        //热门搜索

        Tpl::output('hot_search',@explode(',',C('hot_search')));

        if (C('rec_search') != '') {

            $rec_search_list = @unserialize(C('rec_search'));

        }

        Tpl::output('rec_search_list',is_array($rec_search_list) ? $rec_search_list : array());

        

        //历史搜索

        if (cookie('his_sh') != '') {

            $his_search_list = explode('~', cookie('his_sh'));

        }

        Tpl::output('his_search_list',is_array($his_search_list) ? $his_search_list : array());



        $model_class = Model('goods_class');

        $goods_class = $model_class->get_all_category();

        Tpl::output('show_goods_class',$goods_class);//商品分类



        //获取导航

        Tpl::output('nav_list', rkcache('nav',true));

        //查询保障服务项目

        Tpl::output('contract_list',Model('contract')->getContractItemByCache());

    }



    /**
     * 显示购物车数量
     */
    protected function showCartCount() {

        if (cookie('cart_goods_num') != null){

            $cart_num = intval(cookie('cart_goods_num'));

        }else {

            //已登录状态，存入数据库,未登录时，优先存入缓存，否则存入COOKIE

            if($_SESSION['member_id']) {

                $save_type = 'db';

            } else {

                $save_type = 'cookie';

            }

            $cart_num = Model('cart')->getCartNum($save_type,array('buyer_id'=>$_SESSION['member_id']));//查询购物车商品种类

        }

        Tpl::output('cart_goods_num',$cart_num);

    }

    

    /**
     * 系统公告
     */
    protected function system_notice() {

        $model_message  = Model('article');

        $condition = array();

        $condition['ac_id'] = 1;

        $condition['article_position_in'] = ARTICLE_POSIT_ALL.','.ARTICLE_POSIT_BUYER;

        $condition['limit'] = 5;

        $article_list  = $model_message->getArticleList($condition);

        Tpl::output('system_notice',$article_list);

    }



    /**
     * 输出会员等级
     * @param bool $is_return 是否返回会员信息，返回为true，输出会员信息为false
     */
    protected function getMemberAndGradeInfo($is_return = false){

        $member_info = array();

        //会员详情及会员级别处理

        if($_SESSION['member_id']) {

            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($_SESSION['member_id']);
            if ($member_info){
                $member_gradeinfo = array();
                $member_type_name=$model_member->getMemberTypeName($member_info['member_type']);
                $member_gradeinfo['member_type_name']=$member_type_name;
                $member_info = array_merge($member_info,$member_gradeinfo);
                $member_info['voucher_count'] = Model('voucher')->getCurrentAvailableVoucherCount($_SESSION['member_id']);
                $member_info['redpacket_count'] = Model('redpacket')->getCurrentAvailableRedpacketCount($_SESSION['member_id']);
                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);
            }

        }

        if ($is_return == true){//返回会员信息

            return $member_info;

        } else {//输出会员信息

            Tpl::output('member_info',$member_info);

        }

    }



    /**
     * 验证会员是否登录
     *
     */
    protected function checkLogin(){

        if ($_SESSION['is_login'] !== '1'){

            $ref_url = request_uri();

            if ($_GET['inajax']){

                showDialog('','','js',"login_dialog();",200);

            }else {

                @header("location: " . urlLogin('login', 'index', array('ref_url' => $ref_url)));

            }

            exit;

        }

    }



    //文章输出

    protected function article() {



        if (C('cache_open')) {

            if ($article = rkcache("index/article")) {

                Tpl::output('show_article', $article['show_article']);

                Tpl::output('article_list', $article['article_list']);

                return;

            }

        } else {

            if (file_exists(BASE_DATA_PATH.'/cache/index/article.php')){

                include(BASE_DATA_PATH.'/cache/index/article.php');

                Tpl::output('show_article', $show_article);

                Tpl::output('article_list', $article_list);

                return;

            }

        }



        $model_article_class    = Model('article_class');

        $model_article  = Model('article');

        $show_article = array();//商城公告

        $article_list   = array();//下方文章

        $notice_class   = array('notice');

        $code_array = array('member','store','payment','sold','service','about');

        $notice_limit   = 5;

        $faq_limit  = 5;



        $class_condition    = array();

        $class_condition['home_index'] = 'home_index';

        $class_condition['order'] = 'ac_sort asc';

        $article_class  = $model_article_class->getClassList($class_condition);

        $class_list = array();

        if(!empty($article_class) && is_array($article_class)){

            foreach ($article_class as $key => $val){

                $ac_code = $val['ac_code'];

                $ac_id = $val['ac_id'];

                $val['list']    = array();//文章

                $class_list[$ac_id] = $val;

            }

        }



        $condition  = array();

        $condition['article_show'] = '1';

        $condition['field'] = 'article.article_id,article.ac_id,article.article_url,article_class.ac_code,article.article_position,article.article_title,article.article_time,article_class.ac_name,article_class.ac_parent_id';

        $condition['order'] = 'article_sort asc,article_time desc';

        $condition['limit'] = '300';

        $article_array  = $model_article->getJoinList($condition);

        if(!empty($article_array) && is_array($article_array)){

            foreach ($article_array as $key => $val){

                if ($val['ac_code'] == 'notice' && !in_array($val['article_position'],array(ARTICLE_POSIT_SHOP,ARTICLE_POSIT_ALL))) continue;

                $ac_id = $val['ac_id'];

                $ac_parent_id = $val['ac_parent_id'];

                if($ac_parent_id == 0) {//顶级分类

                    $class_list[$ac_id]['list'][] = $val;

                } else {

                    $class_list[$ac_parent_id]['list'][] = $val;

                }

            }

        }

        if(!empty($class_list) && is_array($class_list)){

            foreach ($class_list as $key => $val){

                $ac_code = $val['ac_code'];

                if(in_array($ac_code,$notice_class)) {

                    $list = $val['list'];

                    array_splice($list, $notice_limit);

                    $val['list'] = $list;

                    $show_article[$ac_code] = $val;

                }

                if (in_array($ac_code,$code_array)){

                    $list = $val['list'];

                    $val['class']['ac_name']    = $val['ac_name'];

                    array_splice($list, $faq_limit);

                    $val['list'] = $list;

                    $article_list[] = $val;

                }

            }

        }

        if (C('cache_open')) {

            wkcache('index/article', array(

            'show_article' => $show_article,

            'article_list' => $article_list,

            ));

        } else {

            $string = "<?php\n\$show_article=".var_export($show_article,true).";\n";

            $string .= "\$article_list=".var_export($article_list,true).";\n?>";

            file_put_contents(BASE_DATA_PATH.'/cache/index/article.php',($string));

        }



        Tpl::output('show_article',$show_article);

        Tpl::output('article_list',$article_list);

    }

    

    /**
     * 自动登录
     */ 
    protected function auto_login() {

        $data = cookie('auto_login');

        if (empty($data)) {

            return false;

        }

        $model_member = Model('member');

        if ($_SESSION['is_login']) {

            $model_member->auto_login();

        }

        $member_id = intval(decrypt($data, MD5_KEY));

        if ($member_id <= 0) {

            return false;

        }

        $member_info = $model_member->getMemberInfoByID($member_id);

        $model_member->createSession($member_info);

    }

}

class BaseLoginControl extends Control{

    /**
     * 构造函数
     */
    public function __construct(){

        /**
         * 读取通用、布局的语言包
         */
        Language::read('common,core_lang_index');

        /**
         * 设置布局文件内容
         */
        Tpl::setLayout('login_layout');



        /**
         * 获取导航
         */
        Tpl::output('nav_list', rkcache('nav',true));

        

        /**
         * 自动登录
         */
        $this->auto_login();

    }



}



class BaseMemberControl extends Control {

    protected $member_info = array();   // 会员信息

    public function __construct(){

        if(!C('site_status')) halt(C('closed_reason'));

        Language::read('common,member_layout');
        //会员验证

        $this->checkLogin();

        //输出头部的公用信息

        $this->showLayout();

        Tpl::setLayout('member_layout');



        //获得会员信息
        $this->member_info = $this->getMemberAndGradeInfo(true);
        Tpl::output('member_info', $this->member_info);
		

        // 左侧导航

        $menu_list = $this->_getMenuList();
         if($this->member_info['member_type']!=4)
        {
            unset($menu_list['info']['child']['member']);
        }
        
         if($this->member_info['member_type']!=5)
        {
            unset($menu_list['property']['child']['member_form_reward']);
        }
        
         if($this->member_info['member_type']!=5)
        {
            unset($menu_list['property']['child']['member_network']);
        }
        
         if($this->member_info['member_type']!=5)
        {
            unset($menu_list['property']['child']['member_node']);
        }
        if($this->member_info['member_type']!=5)
        {
            unset($menu_list['property']['child']['coin']);
        }
        
        
        Tpl::output('menu_list', $menu_list);



        // 系统消息

        $this->system_notice();

        // 页面高亮

        Tpl::output('act', $_GET['act']);

        /**

         * 文章

         */

        $this->article();

    }

    

    /**
     * 左侧导航
     * 菜单数组中child的下标要和其链接的act对应。否则面包屑不能正常显示
     * @return array
     */

    private function _getMenuList() {

        $menu_list = array(

            'info' => array('name' => '会员资料', 'child' => array(

                'member_information'=> array('name' => '账户信息', 'url'=>urlMember('member_information', 'member')),
                'member_tj'         => array('name' => '下级会员', 'url'=>urlMember('member_information', 'membertj')),
                'member'            => array('name' => '申请报单中心', 'url'=>urlMember('member', 'applyform')),
                'member_security'   => array('name' => '账户安全', 'url'=>urlMember('member_security', 'index')),
                'member_address'    => array('name' => '收货地址', 'url'=>urlMember('member_address', 'address')),
                'member_message'    => array('name' => '我的消息', 'url'=>urlMember('member_message', 'message')),
                'member_snsfriend'  => array('name' => '我的好友', 'url'=>urlMember('member_snsfriend', 'find')),
                //'member_bind'       => array('name' => '第三方账号登录', 'url'=>urlMember('member_bind', 'qqbind')),
                //'member_sharemanage'=> array('name' => '分享绑定', 'url'=>urlMember('member_sharemanage', 'index'))

            )),

            'property' => array('name' => '财产中心', 'child' => array(

                'consume'               => array('name' => '消费记录', 'url'=>urlMember('consume', 'index')),
                'predeposit'            => array('name' => '账户余额', 'url'=>urlMember('predeposit', 'pd_log_list')),
                'member_sale'           => array('name' => '销售记录', 'url'=>urlMember('member_sale', 'index')),
                'member_brokerage'      => array('name' => '我的佣金', 'url'=>urlMember('member_brokerage', 'index')),
               
                'member_consume_bean'   => array('name' => '我的消费豆', 'url'=>urlMember('member_consume_bean','index')),
                'member_share_bean'     => array('name' => '我的分红豆', 'url'=>urlMember('member_share_bean', 'index')),
                'member_stock_right'    => array('name' => '我的股权', 'url'=>urlMember('member_stock_right', 'index')),
                'coin'                  => array('name' => '我的报单币', 'url'=>urlMember('coin', 'coin_log_list')),
                
                'member_share_reward'   => array('name' => '我的分享奖', 'url'=>urlMember('member_share_reward', 'index')),
                'member_dynamic_reward' => array('name' => '我的动态奖', 'url'=>urlMember('member_dynamic_reward', 'index')),
                'member_welfare_reward' => array('name' => '我的福利奖', 'url'=>urlMember('member_welfare_reward', 'index')),
                'member_form_reward'    => array('name' => '我的报单奖', 'url'=>urlMember('member_form_reward', 'index')),
                'member_network'        => array('name' => '推荐关系图', 'url'=>urlMember('member_network', 'index')),
                'member_node'           => array('name' => '节点关系图', 'url'=>urlMember('member_node', 'index')),
                //'member_voucher'    => array('name' => '我的代金券', 'url'=>urlMember('member_voucher', 'index')),
                //'member_redpacket'  => array('name' => '我的红包', 'url'=>urlMember('member_redpacket', 'index'))


                

            ))

        );
        
        return $menu_list;

    }

}

class BaseArticleControl extends Control{

    /**

     * 构造函数

     */

    public function __construct(){

        /**

         * 读取通用、布局的语言包

         */

        Language::read('common,core_lang_index');

        /**

         * 设置布局文件内容

        */

        Tpl::setLayout('article_layout');



        /**

         * 获取导航

        */

        Tpl::output('nav_list', rkcache('nav',true));



        /**

         *  输出头部的公用信息

        */

        $this->showLayout();

        

        /**

         * 文章

         */

        $this->article();

    }



}

